﻿using GGGK.Film.IRepository;
using GGGK.Film.IService;
using GGGK.Film.Models.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GGGK.Film.Service
{
    public class UserRoleService : BaseService<Base_UserRole>, IUserRoleService
    {
        private readonly IUserRoleRepository repository;

        public UserRoleService(IUserRoleRepository repository, IBaseQueryRepository baseQuery) : base(repository, baseQuery)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 授权
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public bool Add(int userId, IEnumerable<Base_UserRole> entitys)
        {
            var data = repository.Query(o=>o.UserId==userId);

            foreach (var item in data)
            {
                item.Status = 0;
                repository.Update(item);
            }

            if (entitys.Count() == 0)
            {
                return repository.SaveChanges();
            }

            foreach (var item in entitys)
            {
                repository.Add(item);
            }

            return repository.SaveChanges();
        }
    }
}
